package com.appkubes.fintech.restful.application.rent.controller;


import com.alibaba.fastjson.JSON;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.restful.application.pay.common.contract.response.PayModelResponse;
import com.appkubes.fintech.restful.application.pay.common.contract.response.PayQueryResponse;
import com.appkubes.fintech.restful.application.pay.common.contract.response.RentOrderResponse;
import com.appkubes.fintech.restful.application.pay.wechat.service.WeChatPayService;
import com.appkubes.fintech.restful.application.rent.reponse.BillResponse;
import com.appkubes.fintech.restful.application.rent.request.RepayBeforeRequest;
import com.appkubes.fintech.restful.application.rent.request.RepayPeriodRequest;
import com.appkubes.fintech.restful.application.rent.service.IRePayService;
import com.appkubes.fintech.restful.application.rent.service.IRentBillService;
import com.appkubes.fintech.restful.application.rent.service.IRentLinesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Jiahong Chen
 * @date 2019/11/23
 */

@Api(tags = "账单模块")
@Slf4j
@RequestMapping("rent")
@RestController
public class RentBillController {

    @Autowired
    private IRentBillService billService;
    @Autowired
    private IRePayService rePayService;
    @Autowired
    private IRentLinesService rentLinesService;

    @GetMapping("bill")
    @ApiOperation("账单详情")
    public Response<BillResponse> billDetail(String billKey) {
        return
                billService.getBillInfo(billKey);
    }

    @GetMapping("billList")
    @ApiOperation("账单列表")
    public Response<List<BillResponse>> billList(String userKey) {
        Response<List<BillResponse>> listResponse= billService.queryBillInfo(userKey);
        return listResponse;
    }


    @ApiOperation("账期还款")
    @PostMapping(value = "doPeriodPay")
    public Response<PayModelResponse> doPeriodPay(@RequestBody RepayPeriodRequest request, HttpServletRequest servletRequest) {
     log.info("请求参数peroidPay{}",request);
     if(request.getPayAmt()==null){
            return Response.fail("还款金额不能为空");
        }
        if(StringUtils.isBlank(request.getPayType())){
            return Response.fail("支付通道不能为空");
        }
        if(StringUtils.isBlank(request.getPayModel())){
            return Response.fail("支付类型不能空");
        }
        if(request.getPeriod()==null){
            return Response.fail("还款期数不能为空");
        }
        return rePayService.doPay(request, servletRequest);
    }

    @ApiOperation("提前还款")
    @PostMapping(value = "doBeforePay")
    public Response<PayModelResponse> doBeforePay(@RequestBody RepayBeforeRequest request, HttpServletRequest servletRequest) {
        log.info("请求参数doBeforePay{}",request);
        if(request.getPayAmt()==null){
            return Response.fail("还款金额不能为空");
        }
        if(StringUtils.isBlank(request.getPayType())){
            return Response.fail("支付通道不能为空");
        }
        if(StringUtils.isBlank(request.getPayModel())){
            return Response.fail("支付类型不能空");
        }
        return rePayService.doPay(request, servletRequest);
    }


    @ApiOperation(value = "NPC接收支付异步回调接口", httpMethod = "POST", notes = "所有回掉方法")
    @RequestMapping(value = "notify/{periodType}/{payType}/{orderNo}")
    public void notify(HttpServletRequest request, HttpServletResponse response, @PathVariable String periodType, @PathVariable String payType, @PathVariable String orderNo) {
        log.info("通知回调参数request{} periodType{}, payType{}, orderNo{}", request, periodType, payType, orderNo);
        rePayService.notify(request, response, periodType, payType, orderNo);
    }

    @ApiOperation("支付状态查询接口 还款单ID")
    @GetMapping(value = "queryPay")
    public Response<PayQueryResponse> queryPay(String orderId) {
        log.info("进入chax...." + orderId);
        Response res = rePayService.queryPay(orderId);
        log.info("查询状态.." + JSON.toJSONString(res));
        return res;
    }


    @ApiOperation("账期还款")
    @GetMapping(value = "testPay")
    public Response doPeriodPay(HttpServletRequest servletRequest) {
        Integer period= rentLinesService.queryBeforPeriod("a00494bd3add4e5eb1acb83e880823f3",2);
        System.out.println(period+" 期数");
        return Response.success();
    }


}
